<?php
/**
 * Created by PhpStorm.
 * User: 仙女大人
 * Date: 2019/6/13
 * Time: 14:41
 */

namespace app\admin\controller;
use think\Controller;
use think\Session;
use think\Db;
use app\admin\model\User as UserModel;

class UserList extends Base
{
    //管理员列表
    public function userList(){
        $name = input('session.title');
        $this->assign('title',$name);
        $data = UserModel::paginate(3);
        $this->assign('data',$data);
        return view();
    }

    //管理员列表接口
    public function doUserList(){
        $data = UserModel::paginate(9);
        $list = $data->toArray();
        return json(['code'=>0,'msg'=>"",'count'=>$list['total'],'data'=>$list['data']]);
    }

    // 添加后台管理员
    public function addUser()
    {
        $name = input('session.name');
        $this->assign('name',$name);
        $data = UserModel::paginate(3);
        $this->assign('data',$data);
        return view();
    }

    //处理添加后台管理员
    public function doAdduser(){
        $data = input();
        halt($data);
        $data['like'] = implode(',',array_keys($data['like']));
        $data['password'] = md5($data['password']) ;
        $user = new UserModel();
        if ($user->allowfield(true)->save($data)) {
            return json(['code' => 1, 'data' => $user, 'msg' => 'ok']);
        } else {
            return json(['code' => 2, 'msg' => 'no']);
        }
    }
}